package com.baidu.fsg.uid.config;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baidu.fsg.uid.IdMappingService;
import com.baidu.fsg.uid.impl.IdMappingServiceImpl;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.List;

public class IdMappingSerializer extends JsonSerializer<Object> {

  IdMappingService idMappingService;

  public IdMappingSerializer(){
    this.idMappingService = IdMappingServiceImpl.getIdMappingService();
  }

  @Override
  public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
    if(IdMappingDisableConfiguration.isDisable.get() != null && IdMappingDisableConfiguration.isDisable.get()){
      if(value instanceof String){
        gen.writeString(String.valueOf(value));
      }else if(value instanceof List){
        List<String> ids = (List) value;
        gen.writeStartArray();
        ids.forEach(id->{
          try {
            gen.writeString(id);
          } catch (IOException e) {
            e.printStackTrace();
          }
        });
        gen.writeEndArray();
      }
      if(value.getClass().isArray()){
        gen.setCurrentValue(value);
        if(value.getClass().getComponentType().equals(String.class)){
          String[] v = (String[]) value;
          gen.writeStartArray();
          for (int i = 0; i < v.length; ++i) {
            gen.writeString(v[i]);
          }
          gen.writeEndArray();
        }
      }
      return;
    }
    if(ObjectUtil.isNull(value)){
      gen.writeString(String.valueOf(value));
      return;
    }
    String fieldName = gen.getOutputContext().getCurrentName();
    if(value instanceof String){
      gen.writeString(String.valueOf(value));
      String uuid = idMappingService.getUUID((String) value);
      if(uuid==null){
        return;
      }
      gen.writeStringField(fieldName+"_uuid", uuid);
      return;
    }
    if(value instanceof List){
      List<String> ids = (List) value;
      gen.writeStartArray();
      ids.forEach(id->{
        try {
          gen.writeString(id);
        } catch (IOException e) {
          e.printStackTrace();
        }
      });
      gen.writeEndArray();
      gen.writeFieldName(fieldName+"_uuid");
      List<String> uuids = idMappingService.getUUID(ids);
//      List<String> uuids = ids.stream().map(id->id+"_c").collect(Collectors.toList());
      gen.writeStartArray();
      uuids.forEach(uuid->{
        try {
          gen.writeString(uuid);
        } catch (IOException e) {
          e.printStackTrace();
        }
      });
      gen.writeEndArray();
      return;
    }
    if(value.getClass().isArray()){
      gen.setCurrentValue(value);
      if(value.getClass().getComponentType().equals(String.class)){
        String[] v = (String[]) value;
        gen.writeStartArray();
        for (int i = 0; i < v.length; ++i) {
          gen.writeString(v[i]);
        }
        gen.writeEndArray();

        String[] uuid = idMappingService.getUUID(CollUtil.toList(v)).toArray(new String[0]);
        gen.writeFieldName(fieldName+"_uuid");
        gen.writeStartArray();
        for (int i = 0; i < uuid.length; ++i) {
          gen.writeString(uuid[i]);
        }
        gen.writeEndArray();
      }
    }
  }
}
